﻿using System;
using SceneUnpacker.Schematrix;

namespace SceneUnpacker.se.gno
{
    public class Statistics
    {
        private long _bytesExtracted;
        private DateTime _ended = _zeroTime;
        private ExtractionProgressEventArgs _lastSample;
        private string _name;
        private double _percentComplete;
        private Statistics _split;
        private DateTime _started = _zeroTime;
        private static readonly TimeSpan _zeroSpan = new TimeSpan(0L);
        private static readonly DateTime _zeroTime = new DateTime(0L);

        public Statistics(string name)
        {
            this._name = name;
        }

        public void AddSample(ExtractionProgressEventArgs progress)
        {
            if (this._split != null)
            {
                this._split.AddSample(progress);
            }
            this._lastSample = progress;
            this._percentComplete = progress.PercentComplete;
            this._bytesExtracted += progress.DataLength;
        }

        internal void Begin()
        {
            this._started = DateTime.Now;
            this._ended = _zeroTime;
            this._split = null;
            this._bytesExtracted = 0L;
            this._percentComplete = 0.0;
            this._lastSample = null;
        }

        ~Statistics()
        {
        }

        public void InitSplit(string name)
        {
            if (this._split == null)
            {
                this.Begin();
            }
            this._split = new Statistics(name);
            this._split.Begin();
        }

        public bool IsStarted()
        {
            return this._started.Equals(_zeroTime);
        }

        public double Bandwidth
        {
            get
            {
                TimeSpan timeElapsed = this.TimeElapsed;
                if (timeElapsed == _zeroSpan)
                {
                    return 0.0;
                }
                return ((((double) this.BytesExtracted) / timeElapsed.TotalMilliseconds) * 1000.0);
            }
        }

        public long BytesExtracted
        {
            get
            {
                return this._bytesExtracted;
            }
        }

        public long FileSize
        {
            get
            {
                if (this._lastSample == null)
                {
                    return 0L;
                }
                return this._lastSample.FileSize;
            }
        }

        public double PercentCompleate
        {
            get
            {
                return this._percentComplete;
            }
        }

        public Statistics Split
        {
            get
            {
                return this._split;
            }
        }

        public TimeSpan TimeElapsed
        {
            get
            {
                if (!(this._started == _zeroTime))
                {
                    return (TimeSpan) (((this._ended == _zeroTime) ? DateTime.Now : this._ended) - this._started);
                }
                return _zeroSpan;
            }
        }
    }
}

